// pages/booking/booking.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    // 获取当前的日期
    currentDate: "",
    // 格式化一个 月 日的格式
    layoutCurrentDate: "",

    // 限制选择超前日期
    enddate: "",
    startDate: "",

    // 根据日期查询记账数据的返回数据
    listdata: '',

    // 记录当天几笔记账记录
    count: "",
    // 当天的支出
    expendmoney: 0,
    // 当天的收入
    incomemoney: 0,

    // 当月的收入支出数据
    currentMonthData: {}
  },

  // 获取当月的数据
  getCurrentMonthData() {
    // 加载图标
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.callFunction({
      name: "get_currentMonthData",
      data: {
        endDate: this.data.enddate,
        startDate: this.data.startDate
      }
    }).then(res => {
      // 关闭加载
      wx.hideLoading()
      this.currentMonthData(res.result.data)
      
    }).catch(err => {
      console.log(err);
    })
  },

  // 获取当前时间的方法
  currenDate() {
    let date = new Date();
    let y = date.getFullYear();
    let m = date.getMonth() + 1;
    let d = date.getDate();
    if (m < 10) {
      m = `0${m}`
    }
    if (d < 10) {
      d = `0${d}`
    }
    let currentDate = `${y}-${m}-${d}`
    let layoutCurrentDate = `${m}月${d}号`
    this.setData({
      currentDate: currentDate,
      layoutCurrentDate: layoutCurrentDate
    })
    this.listData()
  },

  // 修改日期的方法
  changedate(e) {
    let time = e.detail.value.split("-")
    let layoutCurrentDate = `${time[1]}月${time[2]}号`
    this.setData({
      currentDate: e.detail.value,
      layoutCurrentDate: layoutCurrentDate
    })
    this.listData()
  },

  // 限制选择日期开始结束的限制
  endDateAndStartDate() {
    let date = new Date();
    let y = date.getFullYear();
    let m = date.getMonth() + 1;
    let d = date.getDate();
    if (m < 10) {
      m = `0${m}`
    }
    if (d < 10) {
      d = `0${d}`
    }
    let enddate = `${y}-${m}-${d}`
    let startDate = `${y}-${m}-01`
    this.setData({
      enddate: enddate,
      startDate: startDate
    })
  },

  // 根据日期查询记账数据
  listData() {
    // 加载图标
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.callFunction({
      name: "get_booking_bydata",
      data: {
        date: this.data.currentDate
      }
    }).then(res => {
      // 关闭加载
      wx.hideLoading()
      // 获取当天有几笔记录
      let count = res.result.data.length;
      // 调用获取当天分别收入和支出的金额的方法
      this.currenDayIncomeAndExpend(res.result.data)

      // 修改数据
      this.setData({
        listdata: res.result.data,
        count: count
      })
    }).catch(err => {
      
    })
  },

  // 获取当天分别收入和支出的金额
  currenDayIncomeAndExpend(data) {
    // 当天收入的钱
    let incomemoney = 0;
    // 当天支出的钱
    let expendmoney = 0;
    // 遍历从list传过来的参数
    for (let i = 0; i < data.length; i++) {
      if (data[i].maintype.type === "sr") {
        incomemoney += (data[i].money - 0)
      }
      if (data[i].maintype.type === "zc") {
        expendmoney += (data[i].money - 0)
      }
    };
    // 处理小数
    incomemoney = this.addThousands(incomemoney.toFixed(2)) ;
    expendmoney = this.addThousands(expendmoney.toFixed(2));
    this.setData({
      incomemoney,
      expendmoney
    })
  },

  // 获取当月收入支出的数据
  currentMonthData(data) {
    // 当月收入的钱
    let incomemoney = 0;
    // 当月收入的钱的小数部位
    let incomemoneydecimal = 0;
    // 当月支出的钱
    let expendmoney = 0;
    // 当月支出的钱的小数部位
    let expendmoneydecimal = 0;
    // 当月结余
    let remainmoney = 0
    // 当月结余的小数部位
    let remainmoneydecimal = 0;
    // 遍历从list传过来的参数
    for (let i = 0; i < data.length; i++) {
      if (data[i].maintype.type === "sr") {
        incomemoney += (data[i].money - 0)
      }
      if (data[i].maintype.type === "zc") {
        expendmoney += (data[i].money - 0)
      }
    };
    // 当月的结余
    remainmoney = (incomemoney - 0) - (expendmoney - 0)
    // 当月数据小数部位赋值
    incomemoneydecimal = incomemoney.toFixed(2).split(".")[1];
    expendmoneydecimal = expendmoney.toFixed(2).split(".")[1];
    remainmoneydecimal = remainmoney.toFixed(2).split(".")[1];
    // 当月数据整数部分
    incomemoney = this.qfw(String(incomemoney).split(".")[0]);
    expendmoney = this.qfw(String(expendmoney).split(".")[0]);
    remainmoney = this.qfw(String(remainmoney).split(".")[0]);
    // 整理成对象
    let currentMonthData = {
      incom: {
        int: incomemoney,
        decimal: incomemoneydecimal
      },
      expend: {
        int: expendmoney,
        decimal: expendmoneydecimal
      },
      remain: {
        int: remainmoney,
        decimal: remainmoneydecimal
      }
    }

    this.setData({
      currentMonthData
    })
  },

  // 千分位处理
  qfw(num) {
    return (num || 0).replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
  },

  // 包括小数位的千分位处理
  addThousands(num) {
    var reg = /\d{1,3}(?=(\d{3})+$)/g;
    if (num && num.toString().indexOf('.') == -1) {
      return (num + '').replace(reg, '$&,');
    } else {
      return num.toString().replace(/(\d)(?=(\d{3})+\.)/g, function ($0, $1) {
        return $1 + ",";
      });
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 获取开始和结束时间
    this.endDateAndStartDate()

    // 调用获取当前时间的方法
    this.currenDate()

    // 调用本月数据的方法
    this.getCurrentMonthData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})